<template>
  <div class="form-loader">
    <component :is="comName"></component>
  </div>
</template>

<script>
export default {
  name: 'FormLoader',
  props: {
    name: String,
    listener: {
      type: Object,
      default() {
        return {}
      }
    },
    injectData: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      comName: '',
      curIns: null
    }
  },
  watch: {
    name: {
      handler(v) {
        this.comName = v
      },
      immediate: true
    }
  },
  provide() {
    let self = this;
    return {
      zformLoader: {
        getCurIns() {
          return self.curIns
        },
        registerCur(ctx) {
          // console.log('registerCur')
          self.curIns = ctx
          self.$emit('loaded', {
            ctx,
            context: self,
            exportCtx: self.curIns.exportCtx
          })
        },
        reset(scope, e) {
          console.log('reset', scope, self.curIns)
          self.curIns.resetFields()
        },
        emit(...args) {
          // console.log(...args)
          if (self.listener[args[0]]) {
            self.listener[args[0]](...args.slice(1))
          }
          self.$emit(...args)
          self.$emit('fire', [...args])
        }
      }
    }
  },
  methods: {
    getCurForm() {
      if (this.curIns) {
        // console.log('call formloader getCurform')
        return this.curIns.exportCtx
      }
      return null
    }
  }
}
</script>
